-include rules.mk

$(shell test -d $(BUILDDIR) || mkdir $(BUILDDIR))
$(shell test -d $(BINDIR) || mkdir $(BINDIR))
$(shell test -d $(OBJSDIR) || mkdir $(OBJSDIR))

export CFLAGS 
export OUTPUTNAME OUTPUTDIR OBJSDIR

INCDIR = $(TOPDIR)/include
INCLUDES = $(foreach INCTREE,$(INCDIR),$(addprefix -I, $(INCTREE)))
export INCLUDES

MAKEFILEDIR	=$(TOPDIR)/package

OBJ = $(notdir $(patsubst %.c, %.o,$(foreach OBJTREE,$(MAKEFILEDIR),$(wildcard $(OBJTREE)/*.c)))) \
	   main.o
OBJS := $(foreach INCTREE,$(OBJ),$(addprefix $(OBJSDIR), $(INCTREE)))

$(OUTPUTNAME):main.o
	$(CC) -o $(BINDIR)/$(OUTPUTNAME) $(OBJS) $(CFLAGS)

main.o:main.c
	@for dir in $(MAKEFILEDIR); \
	do \
		if [ -f Makefile ]; \
		then \
		$(MAKE) -C $$dir || exit "$$?"; \
		fi; \
	done
	$(CC) -c -o $(OBJSDIR)/main.o main.c $(CFLAGS) $(INCLUDES)


.PHONY:clean
clean:
	rm -rf $(BUILDDIR)




